//
//  ZTYCandleCell.m
//  ZXKlineDemo
//
//  Created by wanmeizty on 2018/5/11.
//  Copyright © 2018年 郑旭. All rights reserved.
//

#import "ZTYCandleCell.h"
#import "ZXHeader.h"
#import <Masonry.h>

#import "CAShapeLayer+Shadow.h"

@interface ZTYCandleCell()
@property (nonatomic,strong) KlineModel *model;
@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@property (nonatomic,strong) CAShapeLayer *topShadowLayer;
@property (nonatomic,strong) CAShapeLayer *bottomShadowLayer;
//竖直虚线

@property (nonatomic,strong) UILabel *timeLabel;


/**
 *横竖屏方向
 */
//@property (nonatomic,assign) UIInterfaceOrientation orientation;
@end

@implementation ZTYCandleCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createUI];
    }
    return self;
}

- (void)createUI{
    self.shapeLayer = [CAShapeLayer layer];
    [self.layer addSublayer:self.shapeLayer];
    
    self.topShadowLayer = [CAShapeLayer layer];
    [self.layer addSublayer:self.topShadowLayer];
    
    self.bottomShadowLayer = [CAShapeLayer layer];
    [self.layer addSublayer:self.bottomShadowLayer];
    
    [self.contentView addSubview:self.timeLabel];
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.layer.backgroundColor = BackgroundColor.CGColor;
}

- (void)refreshUI:(KlineModel *)model isDrawKline:(BOOL)isDrawKline{
    [self addTimeLabel];
//    if (isDrawKline && !model.isPlaceHolder) {
    
        if (self.isDrawDottedLine){
            self.timeLabel.text = self.model.timeStr;
            self.timeLabel.hidden = NO;
        }else{
            self.timeLabel.text = @"";
            self.timeLabel.hidden = YES;
        }
        _timeLabel.text = self.model.timeStr;
        // 矩形
        [self.shapeLayer drawRect:CGRectMake(model.y+(CandleWidth-self.candyChartHeight)-self.detailDisplayLabelHeight, 2, model.h, self.frame.size.height-4) fillColor:model.fillColor.CGColor strokeColor:model.strokeColor.CGColor];
        
        // 上影线
        [self.topShadowLayer drawStartPoint:CGPointMake(model.y+model.h+(CandleWidth-self.candyChartHeight)-self.detailDisplayLabelHeight, self.frame.size.height/2) endPoint:CGPointMake(model.highestPoint+(CandleWidth-self.candyChartHeight)-self.detailDisplayLabelHeight, self.frame.size.height/2) strokeColor:model.strokeColor.CGColor];
        
        // 下影线
        [self.bottomShadowLayer drawStartPoint:CGPointMake(model.y+(CandleWidth-self.candyChartHeight)-self.detailDisplayLabelHeight, self.frame.size.height/2) endPoint:CGPointMake(model.lowestPoint+(CandleWidth-self.candyChartHeight)-self.detailDisplayLabelHeight, self.frame.size.height/2) strokeColor:model.strokeColor.CGColor];
        
//    }
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.text = self.model.timeStr;
        _timeLabel.backgroundColor = BackgroundColor;
        _timeLabel.font = [UIFont systemFontOfSize:9];
        _timeLabel.textColor = NormalTextColor;
        [self addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (void)addTimeLabel
{
    [self bringSubviewToFront:self.timeLabel];
    CGFloat height = 60;
    CGFloat leftSpace = 0;
    leftSpace = -(height-TimeViewHeight)/2+(MiddleBlankSpace+QuotaChartHeight-2);
    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(TimeViewHeight);
        make.width.mas_equalTo(height);
        make.top.mas_equalTo(self).offset((height-TimeViewHeight)/2+self.frame.size.height/2);
        make.left.mas_equalTo(self).offset(leftSpace);
    }];
    self.timeLabel.transform = CGAffineTransformMakeRotation(M_PI/2);
}

//- (UIInterfaceOrientation)orientation
//{
//    return  [[UIApplication sharedApplication] statusBarOrientation];
//}

+ (CGFloat)cellHeight{
    return CandleWidth;
}

@end
